#pragma once


// Copyright (c) 2010 Manuel Peinado Gallego <manuel.peinado@gmail.com>
// Distributed under the MIT license


#include <alg3d/alg3d.h>


struct Seg
{
    gx::Vec3 origin;         // Seg. mid point
    gx::Vec3 dir;        // Vec3. from "origin" to positive end, normalize_copy 
    double ext;         // Dist. from "origin" to either end
    
    Seg() 
    {
    }
    
    Seg(gx::Vec3 const & p0, gx::Vec3 const & p1)
    {
        dir = p1 - p0;
        dir.normalize(ext) * 0.5;
        origin = (p0 + p1) * 0.5;
    }
    Seg(gx::Vec3 const & origin, gx::Vec3 const & dir, double ext) 
    : origin(origin), dir(dir), ext(ext) 
    {
        assert(dir.norm() > 1e-8);
    }

    gx::Vec3 closestPnt(const gx::Vec3 & point) const;
    double dist2(Seg const & other, 
                 std::pair<gx::Vec3, gx::Vec3> & closestPnts) const;
    
    gx::Vec3 p0() const { return origin - dir * ext; }
    gx::Vec3 p1() const { return origin + dir * ext; }
};

